#include "SuperMenuBtn.h"
#include "SuperApi.h"

#include <QApplication>
#include <QStylePainter>
#include <QAction>
#include <QMouseEvent>

/*
 * WPMenuButton
 */    
CSuperMenuBtn::CSuperMenuBtn (QWidget *parent) :
    QAbstractButton(parent),
    iOrientation(Qt::Horizontal),
    iAction(NULL)
{
    connect(this, SIGNAL(clicked()), this, SLOT(thisClicked()));
    setFocusPolicy(Qt::NoFocus);
	setFixedHeight(50);
}

CSuperMenuBtn::CSuperMenuBtn (const QString &text, Qt::Orientation orientation, QWidget *parent) :
    QAbstractButton(parent),
    iOrientation(orientation),
    iAction(NULL)
{
    if (!text.isEmpty())
        setText(text);

    connect(this, SIGNAL(clicked()), this, SLOT(thisClicked()));
}

void CSuperMenuBtn::setOrientation (Qt::Orientation orientation)
{
    iOrientation = orientation;

    // size policy
    switch (orientation) {
        case Qt::Horizontal:
            setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

            break;

        case Qt::Vertical:
            setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);

            break;
    }

    // redraw and relayout
    update();
    updateGeometry();
}
    
void CSuperMenuBtn::setAction (QAction *action)
{
    iAction = action;

    // use text/icon
    if(iAction)
        {
        setText(iAction->text());
        setIcon(iAction->icon());
        }
}
    
QSize CSuperMenuBtn::sizeHint () const
{
    int w = 0, h = 0;

    // style options
    QStyleOptionButton option;
    initStyleOption(&option);

    // icon
    if (!icon().isNull()) {
        w += option.iconSize.width();
        h = qMax(h, option.iconSize.height());
    }

    // text
    if (!text().isNull()) {
        QFontMetrics fm = fontMetrics();

        QSize fz = fm.size(Qt::TextSingleLine, text());

        w += fz.width();
        h = qMax(h, fz.height());
    }

    // translate?
    if (iOrientation == Qt::Vertical) {
        int tmp;
     
        // transpose   
        tmp = w;
        w = h;
        h = tmp;
    }
    
    // update rect
    option.rect.setSize(QSize(w, h));

    // calc
    QSize sizeHint = style()->sizeFromContents(QStyle::CT_PushButton, &option, QSize(w, h), this);
    
    // expand to minimum size
    sizeHint = sizeHint.expandedTo(QApplication::globalStrut());

    // XXX: cache
    return sizeHint;
}
    
QSize CSuperMenuBtn::minimumSizeHint () const
{
    return sizeHint();
}
    
void CSuperMenuBtn::initStyleOption (QStyleOptionButton *option) const
{
    // init
    option->initFrom(this);
    option->features = QStyleOptionButton::Flat;

    // features
    if (isDown())
        option->state |= QStyle::State_Sunken;

    else
        option->state |= QStyle::State_Raised;

    // elements
    option->text = text();
    option->icon = icon();
    option->iconSize = iconSize();

    if (iOrientation == Qt::Vertical)
        // transpose
        option->rect.setSize(QSize(option->rect.height(), option->rect.width()));
}
   
void CSuperMenuBtn::paintEvent (QPaintEvent *e)
{
    (void) e;

    QStylePainter painter (this);
    
    // style options
    QStyleOptionButton option;
    initStyleOption(&option);

    // translate?
    if (iOrientation == Qt::Vertical) {
        // translate origin to bottom-left (i.e. bottom-right edge once rotated)
        painter.translate(QPoint(0, size().height()));

        // rotate left
        painter.rotate(-90);
    }

    // draw ourselves, translated...
    painter.drawControl(QStyle::CE_PushButton, option);
}

void CSuperMenuBtn::thisClicked ()
{
    if (iAction)
        iAction->trigger();
}
